package charter1;

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line;
import javafx.scene.shape.Ellipse;

public class JingZiGame extends Application {

	private char whoseTurn='X';//标志谁的下棋回合，初始X先下

    private Cell[][] cell=new Cell[3][3];//内部类

    private Label lblStatus=new Label("X's trun to play");//显示游戏状态，初始X先下
    
	@Override
    public void start(Stage primaryStage) {
    	
        GridPane pane=new GridPane();
        for(int i=0;i<3;i++)
            for(int j=0;j<3;j++)
            	{cell[i][j]=new Cell();
                pane.add(cell[i][j],j,i);}
        Button btnewgame=new Button("New Game");
        HBox hbox=new HBox();
        hbox.getChildren().add(btnewgame);
        hbox.setAlignment(Pos.CENTER);
        BorderPane borderPane=new BorderPane();
        borderPane.setCenter(pane);
        borderPane.setBottom(lblStatus);
        borderPane.setTop(hbox);
        //newgame function
        		btnewgame.setOnAction(e->{
        			start(primaryStage);
        			whoseTurn='X';
        		});
        Scene scene=new Scene(borderPane,500,370);
        primaryStage.setTitle("TicTacToe");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public boolean isFull() {
        for(int i=0;i<3;i++)
            for(int j=0;j<3;j++)
                if(cell[i][j].getToken()==' ')
                    return false;
        return true;
    }

    public boolean isWin(char token) {
        for(int i=0;i<3;i++)
            if(cell[i][0].getToken()==token&&cell[i][1].getToken()==token&&cell[i][2].getToken()==token)
                return true;

        for(int j=0;j<3;j++)
            if(cell[0][j].getToken()==token&&cell[1][j].getToken()==token&&cell[2][j].getToken()==token)
                return true;

        if(cell[0][0].getToken()==token&&cell[1][1].getToken()==token&&cell[2][2].getToken()==token)
            return true;

        if(cell[0][2].getToken()==token&&cell[1][1].getToken()==token&&cell[2][0].getToken()==token)
            return true;

        return false;
    }

    public class Cell extends Pane {
        private char token=' ';//用来标记单元格

        public Cell() {
           setStyle("-fx-border-color:red");
           setPrefSize(2000,2000);
           this.setOnMouseClicked(e -> handleMouseClick());
        }

        public char getToken() {
            return token;
        }

        public void setToken(char c) {
            token=c;

            if(token=='X') {
                Line line1=new Line(10,10,this.getWidth()-10,this.getHeight()-10);
                line1.endXProperty().bind(this.widthProperty().subtract(10));
                line1.endYProperty().bind(this.heightProperty().subtract(10));
                Line line2=new Line(10,this.getHeight()-10,this.getWidth()-10,10);
                line2.startYProperty().bind(this.heightProperty().subtract(10));
                line2.endXProperty().bind(this.widthProperty().subtract(10));
                
                getChildren().addAll(line1,line2);
            }
            else if(token=='O') {
                Ellipse ellipse=new Ellipse(this.getWidth()/2,this.getHeight()/2,
                        this.getWidth()/2-10,this.getHeight()/2-10);
                ellipse.centerXProperty().bind(this.widthProperty().divide(2));
                ellipse.centerYProperty().bind(this.heightProperty().divide(2));
                ellipse.radiusXProperty().bind(this.widthProperty().divide(2).subtract(10));
                ellipse.radiusYProperty().bind(this.heightProperty().divide(2).subtract(10));
                ellipse.setStroke(Color.BLACK);
                ellipse.setFill(Color.WHITE);

                getChildren().add(ellipse);
            }
        }

        private void handleMouseClick() {
            //如果单元格为空且游戏还没有结束
            if(token==' '&&whoseTurn!=' ') {
                setToken(whoseTurn);

                if(isWin(whoseTurn)) {
                    lblStatus.setText(whoseTurn+" won! The game is over");
                    whoseTurn=' ';
                }
                else if (isFull()) {
                    lblStatus.setText("NO winner The game is over");
                    whoseTurn=' ';
                }
                else {
                    whoseTurn=(whoseTurn=='X')? 'O' : 'X';
                    lblStatus.setText(whoseTurn+"'s turn");
                }
            }
        }
    }
    public static void main(String[] args) {
        launch(args);
    }

}